home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 205_01 / bg.c < prev    next >
Text File  |  1980-01-01  |  18KB  |  774 lines

  1. /*
  2. HEADER:         CUG205.00;
  3. TITLE:          BG.C - Backgammon II (for MS-DOS);
  4. VERSION:        2.00;
  5. DATE:           10/14/86;
  6. DESCRIPTION:    "Leor Zolman's public domain backgammon
  7.                 for PC/MS-DOS.  Uses ANSI.SYS for screen control and colors.";
  8. KEYWORDS:       games, backgammon;
  9. SYSTEM:         MS-DOS;
  10. FILENAME:       BG.C.;
  11. WARNINGS:       "This program uses the ANSI color standard.  Include the
  12.                  following line in your CONFIG.SYS file at system start.  
  13.                       device = ansi.sys
  14.                 For further details on ANSI.SYS, refer to your DOS manual.
  15.                 The author claims the copyright to the MS-DOS version and
  16.                 authorizes non-commercial use only.";
  17. SEE-ALSO:       CUG102, BGTALK.C, BGDISP.C;
  18. AUTHORS:        Michael Yokoyama;
  19. COMPILERS:      Microsoft v.3.00.;
  20. */
  21.  
  22. #include <conio.h>
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <time.h>
  27.  
  28. #include "bgdisp.c"     /* Display subroutines              */
  29. #include "bgtalk.c"     /* Conversation subroutines         */
  30.  
  31. #define LINT_ARGS 1     /* Enable strong lint-type checking */
  32. #define NIL (-1)
  33. #define NO 0
  34. #define YES 1
  35. #define MAXGMOV 10
  36. #define MAXLINE 150
  37. #define MAXIMOVES 1000
  38.  
  39. /*  Declare all functions which do not return integers */
  40.  
  41. char strips();
  42.  
  43. char c, level;
  44. int i, j, l, m, count;
  45. int die1, die2;               /* Dice                                   */
  46. int mwin, ywin;               /* Rolling count of my wins and your wins */
  47. int red[31];                  /* Array of the board's red pieces        */
  48. int white[31];                /* Array of the board's white pieces      */
  49. int prob[13];                 /* Probability?                           */
  50. int goodmove[MAXGMOV];
  51. int probmove[MAXGMOV];
  52. int imoves;
  53.  
  54. struct {
  55.   int pos[4];
  56.   int mov[4];
  57. } moves[MAXIMOVES];
  58.  
  59. main()
  60. {
  61.   int a, i, k, n, t, go[6];
  62.   int firstmove;                /* Flag == true on first move only       */
  63.   int wrongmove;                /* Flag == true if move is incorrect     */
  64.   long iths;                    /* Hundredths of secs for random seed    */
  65.   char s[MAXLINE];
  66.  
  67.   mwin = ywin = 0;              /* Reset game score                      */
  68.   time(&iths);                  /* Seed the random generator             */
  69.   srand(iths);
  70.   title();
  71.   printf ("Do you want the instructions?\n");
  72.   if ((c = getche()) == 'y')
  73.     help();
  74.   header();
  75.   select();
  76. newgame:
  77.   init();
  78.   firstmove = 1;
  79.   go[5] = NIL;
  80.   message0();
  81. throw:  
  82.   roll();
  83.   printf ("My roll is: %c[37m%d%c[30m, your roll is: %c[31m%d%c[30m\n",27,die1,27,27,die2,27);
  84.   if (die1 == die2) {
  85.     printf ("Tied up, roll again:\n");
  86.     sleep();
  87.     goto throw;
  88.   }
  89.   printf ("\n%s will go first . . .\n\n", die1 > die2 ? "I" : "You");
  90.   printf ("Press any key:\n");
  91.   getch();
  92.   cls();
  93.   if (die2 > die1) 
  94.     goto red;
  95.  
  96. white: 
  97.   if (!firstmove) 
  98.     roll();                 /* Skip roll on first move */
  99.   firstmove = 0;            /* First roll not true anymore */
  100.   order();
  101.   show2();
  102.   if (!canmove(white,red)) {
  103.     message6();
  104.     goto red;
  105.   }
  106.   message5();
  107.   if (nextmove(white,red) == NIL) 
  108.     goto red;
  109.   if (piececount(white,0,24) == 0) {
  110.     sleep();
  111.     cls();
  112.     printf ("\7I won!\n\n");
  113.     ++mwin;
  114.     score();
  115.     if (mwin == ywin)
  116.       printf ("All tied up!  Who's gonna pull ahead?\n");
  117.     else if (mwin > 2 && !ywin)
  118.       printf ("It's about time you managed to win a game!\n");
  119.     else message1();
  120.     goto again;
  121.   }
  122.  
  123. red:
  124.   dboard();
  125.   if (!firstmove) 
  126.     roll();                  /* Skip roll on first move             */
  127.   firstmove = 0;             /* First roll not true anymore         */
  128.   order();
  129.   show1();
  130.   if (!canmove(red,white)) {
  131.     message7();
  132.     getch();
  133.     goto white;
  134.   }
  135. retry:
  136.   message3();
  137.   while (a = kbhit() != 0) {
  138.     time(&iths);
  139.     srand(iths);             /* Exercise the random generator while */
  140.   }                          /* waiting for a move to be made . . . */
  141.   gets(s);                   
  142.  
  143.   if (strips(s) == '?' || strips(s) == 'h') {
  144.     helpscrn();
  145.     printf ("\nDo you want to see all the instructions?\n");
  146.     if ((c = getche()) == 'y')
  147.       help();
  148.     cls();
  149.     dboard();
  150.     show1();
  151.     wrongmove = YES;
  152.   }
  153.   if (strips(s) == 'q') {     /* check for quit command */
  154.     printf ("Aborting game:  Enter Y to confirm\n");
  155.     if ((c = getche()) == 'y')
  156.       goto quit;
  157.     dboard();
  158.     show1();
  159.     wrongmove = YES;
  160.   }
  161.   if (strips(s) == 'b') {     /* check for print board command */
  162.     cls();
  163.     dboard();
  164.     show1();
  165.     wrongmove = YES;
  166.   }
  167.   if (strips(s) != '-' && !isdigit(strips(s))) {
  168.     cls();
  169.     dboard();
  170.     printf ("\7You must move.  ");
  171.     show1();
  172.     wrongmove = YES;
  173.   }
  174.   /* convert commas to spaces */
  175.   for (i = 0; s[i]; i++)
  176.     if (s[i] == ',') 
  177.       s[i] = ' ';
  178.  
  179.   n = sscanf(s,"%d%d%d%d%d",&go[0],&go[1],&go[2],&go[3],&go[4]);
  180.  
  181.   if ((die1  != die2 && n > 2) || n > 4) {
  182.     cls();
  183.     dboard();
  184.     printf ("\7Too many moves.  ");
  185.     show1();
  186.     wrongmove = YES;
  187.   }
  188.  
  189.   if (((die1  != die2) && n < 2) || ((die1 == die2) && n < 4)) {
  190.     if (n == redcount()) 
  191.       goto moveok;
  192.     printf ("\7Ok to skip move(s)?");
  193.     if ((c = getche()) != 'y') {
  194.       cls();
  195.       dboard();
  196.       printf ("\7Enter your moves again:  ");
  197.       show1();
  198.       wrongmove = YES;
  199.     }
  200.     printf ("\n");
  201.   }
  202.  
  203. moveok: 
  204.   go[n] = NIL;
  205.   if (*s == '-') {
  206.     go[0] = -go[0];
  207.     t = die1;
  208.     die1 = die2;
  209.     die2 = t;
  210.   }
  211.   for (k = 0; k<n; k++) {
  212.     if (0 <= go[k] && go[k] <= 24)
  213.       continue;
  214.     else {
  215.       cls();
  216.       dboard();
  217.       printf ("\7You can't move %d.  ",go[k]);
  218.       show1();
  219.       wrongmove = YES;
  220.     }
  221.   }
  222.   if (play(red,white,go)) 
  223.     wrongmove = YES;
  224.   if (wrongmove == YES) {
  225.     wrongmove = NO;
  226.     goto retry;
  227.   }
  228.   if (piececount(red,0,24) == 0) {
  229.     sleep();
  230.     cls();
  231.     printf ("\7You win . . .\n");
  232.     ++ywin;
  233.     score();
  234.     if (mwin == ywin)
  235.       printf ("\nLooks like now we're dead even . . . it's tiebreaker time!\n");
  236.     else if (ywin > 2 && !mwin)
  237.       printf ("\nYou're lucky I don't throw the dice. . . .\n");
  238.     else message2();
  239.  
  240. again:    
  241.     message4();
  242. poll:
  243.     c = getch();
  244.     if (c == 'y') {
  245.       cls();
  246.       goto newgame;
  247.     }
  248.     else if (c != 'n') {
  249.       printf("\7");
  250.       goto poll;
  251.     }
  252.     else goto quit;
  253.   }
  254.   goto white;
  255. quit:
  256.   cls();
  257.   exit(1);
  258. }
  259.  
  260. /* Initialize the board and the probabilities */
  261. init()
  262. {
  263.   int i;
  264.  
  265.   /* Clear off board and enter the starting positions of the pieces */
  266.   for (i = 0; i <= 30; i++)
  267.     red[i] = white[i] = 0;
  268.   red[1] = white[1] = 2;
  269.   red[12] = white[12] = 5;
  270.   red[17] = white[17] = 3;
  271.   red[19] = white[19] = 5;
  272.  
  273.   for (i = 0; i <= 30; i++)   /* Note:  Is this code necessary? */
  274.     prob[i] = 0;
  275.  
  276.   prob[0] = 0;
  277.   prob[1] = 11;
  278.   prob[2] = 12;
  279.   prob[3] = 13;
  280.   prob[4] = 14;
  281.   prob[5] = 15;
  282.   prob[6] = 16;
  283.   prob[7] =  6;
  284.   prob[8] =  5;
  285.   prob[9] =  4;
  286.   prob[10] = 3;
  287.   prob[11] = 2;
  288.   prob[12] = 1;
  289. }
  290.  
  291. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  292. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  293. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  294. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  295. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  296.  
  297. int play(player,opponent,pos)
  298. int *player,*opponent,pos[];
  299. {
  300.   int k,n,die,ipos;
  301.  
  302.   /* Blots on player[0] must be moved first */
  303.   for (k = 0;k<player[0];k++) {
  304.     if (pos[k] == NIL)
  305.       break;
  306.     if (pos[k] != 0) {
  307.       printf ("\nPlay your BAR piece (on point 0) first\n");
  308.       return(-1);
  309.     }
  310.   }
  311.  
  312.   for (k = 0;(ipos = pos[k]) != NIL;k++) {
  313.     die = k?die2:die1;
  314.     n = 25-ipos-die;
  315.     if (player[ipos] == 0) 
  316.       goto illegal;
  317.     if (n>0 && opponent[n] >= 2) 
  318.       goto illegal;
  319.     if (n <= 0) {
  320.       if (piececount(player,0,18) != 0) 
  321.         goto illegal;
  322.       if ((ipos+die) != 25 && piecec